import { apiBaseUrl } from '@/utils/config.js';
const aGet = "GET";
const aPost = "POST";
const aDelete = "DELETE";
const aPut = "PUT";
// put请求
function putRequest(url,data) {
	var promise = new Promise((resolve,reject) => {
		var that = this;
		var putData = data;
		var token = uni.getStorageSync("Authorization");
		uni.request({
			url: `${apiBaseUrl}` + url,
			data: putData,
			method: aPut,
			header: {
				'content-type': 'application/json',
				'Authorization': token
			},
			success:function(res){
				if(res.statusCode == 200) {
					if(res.data.code == 200) {
						resolve(res.data);
					} else if(res.data.code == 400) {
						uni.showToast({
							title: '请求失败',
							icon: 'none',
							duration: 2000
						})
					} else if(res.data.code == 500) {
						uni.showToast({
							title: '系统繁忙，请稍后重试',
							icon: 'none',
							duration: 2000
						})
					}
				}
			},
			error: (error) => {
				uni.showToast({
					title: '请求失败',
					icon: 'none',
					duration: 2000
				})
				reject('请求失败')
			}
		})
	});
	return promise;
}
// delete 请求
function deleteRequest(url,data) {
	var promise = new Promise((resolve,reject) => {
		var that = this;
		var deleteData = data;
		var token = uni.getStorageSync("Authorization");
		uni.request({
			url: `${apiBaseUrl}` + url,
			data: deleteData,
			method: aDelete,
			header: {
				'content-type': 'application/json',
				'Authorization': token
			},
			success:function(res){
				if(res.statusCode == 200) {
					if(res.data.code == 200) {
						resolve(res.data);
					} else if(res.data.code == 400) {
						uni.showToast({
							title: '请求失败',
							icon: 'none',
							duration: 2000
						})
					} else if(res.data.code == 500) {
						uni.showToast({
							title: '系统繁忙，请稍后重试',
							icon: 'none',
							duration: 2000
						})
					}
				}
			},
			error: (error) => {
				uni.showToast({
					title: '请求失败',
					icon: 'none',
					duration: 2000
				})
				reject('请求失败')
			}
		})
	});
	return promise;
}
// post 请求
function postRequest(url,data) {
	var promise = new Promise((resolve,reject) => {
		var that = this;
		var postData = data;
		var token = uni.getStorageSync("Authorization");
		uni.request({
			url: `${apiBaseUrl}` + url,
			data: postData,
			method: aPost,
			header: {
				'content-type': 'application/json',
				'Authorization': token,
			},
			success:function(res){
				if(res.statusCode == 200) {
					if(res.data.code == 200) {
						resolve(res.data);
					} else if(res.data.code == 400) {
						uni.showToast({
							title: '请求失败',
							icon: 'none',
							duration: 2000
						})
					} else if(res.data.code == 500) {
						uni.showToast({
							title: '系统繁忙，请稍后重试',
							icon: 'none',
							duration: 2000
						})
					}
				} 
			},
			error: (error) => {
				uni.showToast({
					title: '请求失败',
					icon: 'none',
					duration: 2000
				})
				reject('请求失败')
			}
		})
	});
	return promise;
}
// get 请求公用
function getComRequest(url,data) {
	var promise = new Promise((resolve,reject) => {
		var that = this;
		var token = uni.getStorageSync("Authorization");
		uni.request({
			url: `${apiBaseUrl}` + url,
			data: data,
			method: aGet,
			header: {
				'content-type': 'application/json',
				'Authorization': token
			},
			success:function(res){
				if(res.statusCode == 200) {
					if(res.data.code == 200) {
						resolve(res.data);
					} else if(res.data.code == 400) {
						uni.showToast({
							title: '请求失败',
							icon: 'none',
							duration: 2000
						})
					} else if(res.data.code == 500) {
						uni.showToast({
							title: '系统繁忙，请稍后重试',
							icon: 'none',
							duration: 2000
						})
					}
				}
			},
			error: (error) => {
				console.log(error)
				uni.showToast({
					title: '请求失败',
					icon: 'none',
					duration: 2000
				})
				reject('请求失败')
			}
		})
	});
	return promise;
}
// get请求
function getRequest(url,data) {
	var promise = new Promise((resolve,reject) => {
		var that = this;
		uni.request({
			url: `${apiBaseUrl}` + url,
			data: data,
			method: aGet,
			header: {
				'content-type': 'application/json',
			},
			success:function(res){
				if(res.statusCode == 200) {
					if(res.data.code == 200) {
						resolve(res.data);
					} else if(res.data.code == 400) {
						uni.showToast({
							title: '请求失败',
							icon: 'none',
							duration: 2000
						})
					} else if(res.data.code == 500) {
						uni.showToast({
							title: '系统繁忙，请稍后重试',
							icon: 'none',
							duration: 2000
						})
					}
				} 
			},
			fail: (error) => {
				uni.showToast({
					title: '请求失败',
					icon: 'none',
					duration: 2000
				})
				reject('请求失败')
			}
		})
	});
	return promise;
}
module.exports = {
	postRequest,
	getRequest,
	getComRequest,
	deleteRequest,
	putRequest
}